<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>textarea.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_textarea.js.html">textarea.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A content-aware textarea control that grows and shrinks
<a name="line17"></a> * automatically. This implementation extends {@link goog.ui.Control}.
<a name="line18"></a> * This code is inspired by Dojo Dijit&#39;s Textarea implementation with
<a name="line19"></a> * modifications to support native (when available) textarea resizing and
<a name="line20"></a> * minHeight and maxHeight enforcement.
<a name="line21"></a> *
<a name="line22"></a> * @see ../demos/textarea.html
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.ui.Textarea&#39;);
<a name="line26"></a>goog.provide(&#39;goog.ui.Textarea.EventType&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line29"></a>goog.require(&#39;goog.dom&#39;);
<a name="line30"></a>goog.require(&#39;goog.dom.classlist&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line32"></a>goog.require(&#39;goog.style&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Control&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.TextareaRenderer&#39;);
<a name="line35"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * A textarea control to handle growing/shrinking with textarea.value.
<a name="line41"></a> *
<a name="line42"></a> * @param {string} content Text to set as the textarea&#39;s value.
<a name="line43"></a> * @param {goog.ui.TextareaRenderer=} opt_renderer Renderer used to render or
<a name="line44"></a> *     decorate the textarea. Defaults to {@link goog.ui.TextareaRenderer}.
<a name="line45"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line46"></a> *     document interaction.
<a name="line47"></a> * @constructor
<a name="line48"></a> * @extends {goog.ui.Control}
<a name="line49"></a> */
<a name="line50"></a>goog.ui.Textarea = function(content, opt_renderer, opt_domHelper) {
<a name="line51"></a>  goog.ui.Control.call(this, content, opt_renderer ||
<a name="line52"></a>      goog.ui.TextareaRenderer.getInstance(), opt_domHelper);
<a name="line53"></a>
<a name="line54"></a>  this.setHandleMouseEvents(false);
<a name="line55"></a>  this.setAllowTextSelection(true);
<a name="line56"></a>  this.hasUserInput_ = (content != &#39;&#39;);
<a name="line57"></a>  if (!content) {
<a name="line58"></a>    this.setContentInternal(&#39;&#39;);
<a name="line59"></a>  }
<a name="line60"></a>};
<a name="line61"></a>goog.inherits(goog.ui.Textarea, goog.ui.Control);
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Some UAs will shrink the textarea automatically, some won&#39;t.
<a name="line66"></a> * @type {boolean}
<a name="line67"></a> * @private
<a name="line68"></a> */
<a name="line69"></a>goog.ui.Textarea.NEEDS_HELP_SHRINKING_ = goog.userAgent.GECKO ||
<a name="line70"></a>    goog.userAgent.WEBKIT;
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * True if the resizing function is executing, false otherwise.
<a name="line75"></a> * @type {boolean}
<a name="line76"></a> * @private
<a name="line77"></a> */
<a name="line78"></a>goog.ui.Textarea.prototype.isResizing_ = false;
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Represents if we have focus on the textarea element, used only
<a name="line83"></a> * to render the placeholder if we don&#39;t have native placeholder
<a name="line84"></a> * support.
<a name="line85"></a> * @type {boolean}
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.ui.Textarea.prototype.hasFocusForPlaceholder_ = false;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * @type {boolean}
<a name="line93"></a> * @private
<a name="line94"></a> */
<a name="line95"></a>goog.ui.Textarea.prototype.hasUserInput_ = false;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * The height of the textarea as last measured.
<a name="line100"></a> * @type {number}
<a name="line101"></a> * @private
<a name="line102"></a> */
<a name="line103"></a>goog.ui.Textarea.prototype.height_ = 0;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * A maximum height for the textarea. When set to 0, the default, there is no
<a name="line108"></a> * enforcement of this value during resize.
<a name="line109"></a> * @type {number}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.ui.Textarea.prototype.maxHeight_ = 0;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * A minimum height for the textarea. When set to 0, the default, there is no
<a name="line117"></a> * enforcement of this value during resize.
<a name="line118"></a> * @type {number}
<a name="line119"></a> * @private
<a name="line120"></a> */
<a name="line121"></a>goog.ui.Textarea.prototype.minHeight_ = 0;
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Whether or not textarea rendering characteristics have been discovered.
<a name="line126"></a> * Specifically we determine, at runtime:
<a name="line127"></a> *    If the padding and border box is included in offsetHeight.
<a name="line128"></a> *    @see {goog.ui.Textarea.prototype.needsPaddingBorderFix_}
<a name="line129"></a> *    If the padding and border box is included in scrollHeight.
<a name="line130"></a> *    @see {goog.ui.Textarea.prototype.scrollHeightIncludesPadding_} and
<a name="line131"></a> *    @see {goog.ui.Textarea.prototype.scrollHeightIncludesBorder_}
<a name="line132"></a> * TODO(user): See if we can determine goog.ui.Textarea.NEEDS_HELP_SHRINKING_.
<a name="line133"></a> * @type {boolean}
<a name="line134"></a> * @private
<a name="line135"></a> */
<a name="line136"></a>goog.ui.Textarea.prototype.hasDiscoveredTextareaCharacteristics_ = false;
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * If a user agent doesn&#39;t correctly support the box-sizing:border-box CSS
<a name="line141"></a> * value then we&#39;ll need to adjust our height calculations.
<a name="line142"></a> * @see {goog.ui.Textarea.prototype.discoverTextareaCharacteristics_}
<a name="line143"></a> * @type {boolean}
<a name="line144"></a> * @private
<a name="line145"></a> */
<a name="line146"></a>goog.ui.Textarea.prototype.needsPaddingBorderFix_ = false;
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Whether or not scrollHeight of a textarea includes the padding box.
<a name="line151"></a> * @type {boolean}
<a name="line152"></a> * @private
<a name="line153"></a> */
<a name="line154"></a>goog.ui.Textarea.prototype.scrollHeightIncludesPadding_ = false;
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Whether or not scrollHeight of a textarea includes the border box.
<a name="line159"></a> * @type {boolean}
<a name="line160"></a> * @private
<a name="line161"></a> */
<a name="line162"></a>goog.ui.Textarea.prototype.scrollHeightIncludesBorder_ = false;
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * For storing the padding box size during enterDocument, to prevent possible
<a name="line167"></a> * measurement differences that can happen after text zooming.
<a name="line168"></a> * Note: runtime padding changes will cause problems with this.
<a name="line169"></a> * @type {goog.math.Box}
<a name="line170"></a> * @private
<a name="line171"></a> */
<a name="line172"></a>goog.ui.Textarea.prototype.paddingBox_;
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * For storing the border box size during enterDocument, to prevent possible
<a name="line177"></a> * measurement differences that can happen after text zooming.
<a name="line178"></a> * Note: runtime border width changes will cause problems with this.
<a name="line179"></a> * @type {goog.math.Box}
<a name="line180"></a> * @private
<a name="line181"></a> */
<a name="line182"></a>goog.ui.Textarea.prototype.borderBox_;
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Default text content for the textarea when it is unchanged and unfocussed.
<a name="line187"></a> * We use the placeholder attribute for all browsers that have support for
<a name="line188"></a> * it (new in HTML5 for the following browsers:
<a name="line189"></a> *
<a name="line190"></a> *   Internet Explorer 10.0
<a name="line191"></a> *   Firefox 4.0
<a name="line192"></a> *   Opera 11.6
<a name="line193"></a> *   Chrome 4.0
<a name="line194"></a> *   Safari 5.0
<a name="line195"></a> *
<a name="line196"></a> * For older browsers, we save the placeholderText_ and set it as the element&#39;s
<a name="line197"></a> * value and add the TEXTAREA_PLACEHOLDER_CLASS to indicate that it&#39;s a
<a name="line198"></a> * placeholder string.
<a name="line199"></a> * @type {string}
<a name="line200"></a> * @private
<a name="line201"></a> */
<a name="line202"></a>goog.ui.Textarea.prototype.placeholderText_ = &#39;&#39;;
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Constants for event names.
<a name="line207"></a> * @enum {string}
<a name="line208"></a> */
<a name="line209"></a>goog.ui.Textarea.EventType = {
<a name="line210"></a>  RESIZE: &#39;resize&#39;
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Sets the default text for the textarea.
<a name="line216"></a> * @param {string} text The default text for the textarea.
<a name="line217"></a> */
<a name="line218"></a>goog.ui.Textarea.prototype.setPlaceholder = function(text) {
<a name="line219"></a>  this.placeholderText_ = text;
<a name="line220"></a>  if (this.getElement()) {
<a name="line221"></a>    this.restorePlaceholder_();
<a name="line222"></a>  }
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * @return {number} The padding plus the border box height.
<a name="line228"></a> * @private
<a name="line229"></a> */
<a name="line230"></a>goog.ui.Textarea.prototype.getPaddingBorderBoxHeight_ = function() {
<a name="line231"></a>  var paddingBorderBoxHeight = this.paddingBox_.top + this.paddingBox_.bottom +
<a name="line232"></a>      this.borderBox_.top + this.borderBox_.bottom;
<a name="line233"></a>  return paddingBorderBoxHeight;
<a name="line234"></a>};
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>/**
<a name="line238"></a> * @return {number} The minHeight value.
<a name="line239"></a> */
<a name="line240"></a>goog.ui.Textarea.prototype.getMinHeight = function() {
<a name="line241"></a>  return this.minHeight_;
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * @return {number} The minHeight value with a potential padding fix.
<a name="line247"></a> * @private
<a name="line248"></a> */
<a name="line249"></a>goog.ui.Textarea.prototype.getMinHeight_ = function() {
<a name="line250"></a>  var minHeight = this.minHeight_;
<a name="line251"></a>  var textarea = this.getElement();
<a name="line252"></a>  if (minHeight &amp;&amp; textarea &amp;&amp; this.needsPaddingBorderFix_) {
<a name="line253"></a>    minHeight -= this.getPaddingBorderBoxHeight_();
<a name="line254"></a>  }
<a name="line255"></a>  return minHeight;
<a name="line256"></a>};
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * Sets a minimum height for the textarea, and calls resize if rendered.
<a name="line261"></a> * @param {number} height New minHeight value.
<a name="line262"></a> */
<a name="line263"></a>goog.ui.Textarea.prototype.setMinHeight = function(height) {
<a name="line264"></a>  this.minHeight_ = height;
<a name="line265"></a>  this.resize();
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/**
<a name="line270"></a> * @return {number} The maxHeight value.
<a name="line271"></a> */
<a name="line272"></a>goog.ui.Textarea.prototype.getMaxHeight = function() {
<a name="line273"></a>  return this.maxHeight_;
<a name="line274"></a>};
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/**
<a name="line278"></a> * @return {number} The maxHeight value with a potential padding fix.
<a name="line279"></a> * @private
<a name="line280"></a> */
<a name="line281"></a>goog.ui.Textarea.prototype.getMaxHeight_ = function() {
<a name="line282"></a>  var maxHeight = this.maxHeight_;
<a name="line283"></a>  var textarea = this.getElement();
<a name="line284"></a>  if (maxHeight &amp;&amp; textarea &amp;&amp; this.needsPaddingBorderFix_) {
<a name="line285"></a>    maxHeight -= this.getPaddingBorderBoxHeight_();
<a name="line286"></a>  }
<a name="line287"></a>  return maxHeight;
<a name="line288"></a>};
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>/**
<a name="line292"></a> * Sets a maximum height for the textarea, and calls resize if rendered.
<a name="line293"></a> * @param {number} height New maxHeight value.
<a name="line294"></a> */
<a name="line295"></a>goog.ui.Textarea.prototype.setMaxHeight = function(height) {
<a name="line296"></a>  this.maxHeight_ = height;
<a name="line297"></a>  this.resize();
<a name="line298"></a>};
<a name="line299"></a>
<a name="line300"></a>
<a name="line301"></a>/**
<a name="line302"></a> * Sets the textarea&#39;s value.
<a name="line303"></a> * @param {*} value The value property for the textarea, will be cast to a
<a name="line304"></a> *     string by the browser when setting textarea.value.
<a name="line305"></a> */
<a name="line306"></a>goog.ui.Textarea.prototype.setValue = function(value) {
<a name="line307"></a>  this.setContent(String(value));
<a name="line308"></a>};
<a name="line309"></a>
<a name="line310"></a>
<a name="line311"></a>/**
<a name="line312"></a> * Gets the textarea&#39;s value.
<a name="line313"></a> * @return {string} value The value of the textarea.
<a name="line314"></a> */
<a name="line315"></a>goog.ui.Textarea.prototype.getValue = function() {
<a name="line316"></a>  // We potentially have the placeholder stored in the value.
<a name="line317"></a>  // If a client of this class sets this.getElement().value directly
<a name="line318"></a>  // we don&#39;t set the this.hasUserInput_ boolean. Thus, we need to
<a name="line319"></a>  // explicitly check if the value != the placeholder text. This has
<a name="line320"></a>  // the unfortunate edge case of:
<a name="line321"></a>  //   If the client sets this.getElement().value to the placeholder
<a name="line322"></a>  //   text, we&#39;ll return the empty string.
<a name="line323"></a>  // The normal use case shouldn&#39;t be an issue, however, since the
<a name="line324"></a>  // default placeholderText is the empty string. Also, if the end user
<a name="line325"></a>  // inputs text, then this.hasUserInput_ will always be true.
<a name="line326"></a>  if (this.getElement().value != this.placeholderText_ ||
<a name="line327"></a>      this.supportsNativePlaceholder_() || this.hasUserInput_) {
<a name="line328"></a>    // We don&#39;t do anything fancy here.
<a name="line329"></a>    return this.getElement().value;
<a name="line330"></a>  }
<a name="line331"></a>  return &#39;&#39;;
<a name="line332"></a>};
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>/** @override */
<a name="line336"></a>goog.ui.Textarea.prototype.setContent = function(content) {
<a name="line337"></a>  goog.ui.Textarea.superClass_.setContent.call(this, content);
<a name="line338"></a>  this.hasUserInput_ = (content != &#39;&#39;);
<a name="line339"></a>  this.resize();
<a name="line340"></a>};
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/** @override **/
<a name="line344"></a>goog.ui.Textarea.prototype.setEnabled = function(enable) {
<a name="line345"></a>  goog.ui.Textarea.superClass_.setEnabled.call(this, enable);
<a name="line346"></a>  this.getElement().disabled = !enable;
<a name="line347"></a>};
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>/**
<a name="line351"></a> * Resizes the textarea vertically.
<a name="line352"></a> */
<a name="line353"></a>goog.ui.Textarea.prototype.resize = function() {
<a name="line354"></a>  if (this.getElement()) {
<a name="line355"></a>    this.grow_();
<a name="line356"></a>  }
<a name="line357"></a>};
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * @return {boolean} True if the element supports the placeholder attribute.
<a name="line362"></a> * @private
<a name="line363"></a> */
<a name="line364"></a>goog.ui.Textarea.prototype.supportsNativePlaceholder_ = function() {
<a name="line365"></a>  goog.asserts.assert(this.getElement());
<a name="line366"></a>  return &#39;placeholder&#39; in this.getElement();
<a name="line367"></a>};
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>/**
<a name="line371"></a> * Sets the value of the textarea element to the default text.
<a name="line372"></a> * @private
<a name="line373"></a> */
<a name="line374"></a>goog.ui.Textarea.prototype.restorePlaceholder_ = function() {
<a name="line375"></a>  if (!this.placeholderText_) {
<a name="line376"></a>    // Return early if there is no placeholder to mess with.
<a name="line377"></a>    return;
<a name="line378"></a>  }
<a name="line379"></a>  // Check again in case something changed since this was scheduled.
<a name="line380"></a>  // We check that the element is still there since this is called by a timer
<a name="line381"></a>  // and the dispose method may have been called prior to this.
<a name="line382"></a>  if (this.supportsNativePlaceholder_()) {
<a name="line383"></a>    this.getElement().placeholder = this.placeholderText_;
<a name="line384"></a>  } else if (this.getElement() &amp;&amp; !this.hasUserInput_ &amp;&amp;
<a name="line385"></a>      !this.hasFocusForPlaceholder_) {
<a name="line386"></a>    // We only want to set the value + placeholder CSS if we actually have
<a name="line387"></a>    // some placeholder text to show.
<a name="line388"></a>    goog.dom.classlist.add(
<a name="line389"></a>        goog.asserts.assert(this.getElement()),
<a name="line390"></a>        goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS);
<a name="line391"></a>    this.getElement().value = this.placeholderText_;
<a name="line392"></a>  }
<a name="line393"></a>};
<a name="line394"></a>
<a name="line395"></a>
<a name="line396"></a>/** @override **/
<a name="line397"></a>goog.ui.Textarea.prototype.enterDocument = function() {
<a name="line398"></a>  goog.ui.Textarea.base(this, &#39;enterDocument&#39;);
<a name="line399"></a>  var textarea = this.getElement();
<a name="line400"></a>
<a name="line401"></a>  // Eliminates the vertical scrollbar and changes the box-sizing mode for the
<a name="line402"></a>  // textarea to the border-box (aka quirksmode) paradigm.
<a name="line403"></a>  goog.style.setStyle(textarea, {
<a name="line404"></a>    &#39;overflowY&#39;: &#39;hidden&#39;,
<a name="line405"></a>    &#39;overflowX&#39;: &#39;auto&#39;,
<a name="line406"></a>    &#39;boxSizing&#39;: &#39;border-box&#39;,
<a name="line407"></a>    &#39;MsBoxSizing&#39;: &#39;border-box&#39;,
<a name="line408"></a>    &#39;WebkitBoxSizing&#39;: &#39;border-box&#39;,
<a name="line409"></a>    &#39;MozBoxSizing&#39;: &#39;border-box&#39;});
<a name="line410"></a>
<a name="line411"></a>  this.paddingBox_ = goog.style.getPaddingBox(textarea);
<a name="line412"></a>  this.borderBox_ = goog.style.getBorderBox(textarea);
<a name="line413"></a>
<a name="line414"></a>  this.getHandler().
<a name="line415"></a>      listen(textarea, goog.events.EventType.SCROLL, this.grow_).
<a name="line416"></a>      listen(textarea, goog.events.EventType.FOCUS, this.grow_).
<a name="line417"></a>      listen(textarea, goog.events.EventType.KEYUP, this.grow_).
<a name="line418"></a>      listen(textarea, goog.events.EventType.MOUSEUP, this.mouseUpListener_).
<a name="line419"></a>      listen(textarea, goog.events.EventType.BLUR, this.blur_);
<a name="line420"></a>
<a name="line421"></a>  this.restorePlaceholder_();
<a name="line422"></a>  this.resize();
<a name="line423"></a>};
<a name="line424"></a>
<a name="line425"></a>
<a name="line426"></a>/**
<a name="line427"></a> * Gets the textarea&#39;s content height + padding height + border height.
<a name="line428"></a> * This is done by getting the scrollHeight and adjusting from there.
<a name="line429"></a> * In the end this result is what we want the new offsetHeight to equal.
<a name="line430"></a> * @return {number} The height of the textarea.
<a name="line431"></a> * @private
<a name="line432"></a> */
<a name="line433"></a>goog.ui.Textarea.prototype.getHeight_ = function() {
<a name="line434"></a>  this.discoverTextareaCharacteristics_();
<a name="line435"></a>  var textarea = this.getElement();
<a name="line436"></a>  // Because enterDocument can be called even when the component is rendered
<a name="line437"></a>  // without being in a document, we may not have cached the correct paddingBox
<a name="line438"></a>  // data on render(). We try to make up for this here.
<a name="line439"></a>  if (isNaN(this.paddingBox_.top)) {
<a name="line440"></a>    this.paddingBox_ = goog.style.getPaddingBox(textarea);
<a name="line441"></a>    this.borderBox_ = goog.style.getBorderBox(textarea);
<a name="line442"></a>  }
<a name="line443"></a>  // Accounts for a possible (though unlikely) horizontal scrollbar.
<a name="line444"></a>  var height = this.getElement().scrollHeight +
<a name="line445"></a>      this.getHorizontalScrollBarHeight_();
<a name="line446"></a>  if (this.needsPaddingBorderFix_) {
<a name="line447"></a>    height -= this.getPaddingBorderBoxHeight_();
<a name="line448"></a>  } else {
<a name="line449"></a>    if (!this.scrollHeightIncludesPadding_) {
<a name="line450"></a>      var paddingBox = this.paddingBox_;
<a name="line451"></a>      var paddingBoxHeight = paddingBox.top + paddingBox.bottom;
<a name="line452"></a>      height += paddingBoxHeight;
<a name="line453"></a>    }
<a name="line454"></a>    if (!this.scrollHeightIncludesBorder_) {
<a name="line455"></a>      var borderBox = goog.style.getBorderBox(textarea);
<a name="line456"></a>      var borderBoxHeight = borderBox.top + borderBox.bottom;
<a name="line457"></a>      height += borderBoxHeight;
<a name="line458"></a>    }
<a name="line459"></a>  }
<a name="line460"></a>  return height;
<a name="line461"></a>};
<a name="line462"></a>
<a name="line463"></a>
<a name="line464"></a>/**
<a name="line465"></a> * Sets the textarea&#39;s height.
<a name="line466"></a> * @param {number} height The height to set.
<a name="line467"></a> * @private
<a name="line468"></a> */
<a name="line469"></a>goog.ui.Textarea.prototype.setHeight_ = function(height) {
<a name="line470"></a>  if (this.height_ != height) {
<a name="line471"></a>    this.height_ = height;
<a name="line472"></a>    this.getElement().style.height = height + &#39;px&#39;;
<a name="line473"></a>  }
<a name="line474"></a>};
<a name="line475"></a>
<a name="line476"></a>
<a name="line477"></a>/**
<a name="line478"></a> * Sets the textarea&#39;s rows attribute to be the number of newlines + 1.
<a name="line479"></a> * This is necessary when the textarea is hidden, in which case scrollHeight
<a name="line480"></a> * is not available.
<a name="line481"></a> * @private
<a name="line482"></a> */
<a name="line483"></a>goog.ui.Textarea.prototype.setHeightToEstimate_ = function() {
<a name="line484"></a>  var textarea = this.getElement();
<a name="line485"></a>  textarea.style.height = &#39;auto&#39;;
<a name="line486"></a>  var newlines = textarea.value.match(/\n/g) || [];
<a name="line487"></a>  textarea.rows = newlines.length + 1;
<a name="line488"></a>};
<a name="line489"></a>
<a name="line490"></a>
<a name="line491"></a>/**
<a name="line492"></a> * Gets the the height of (possibly present) horizontal scrollbar.
<a name="line493"></a> * @return {number} The height of the horizontal scrollbar.
<a name="line494"></a> * @private
<a name="line495"></a> */
<a name="line496"></a>goog.ui.Textarea.prototype.getHorizontalScrollBarHeight_ =
<a name="line497"></a>    function() {
<a name="line498"></a>  var textarea = this.getElement();
<a name="line499"></a>  var height = textarea.offsetHeight - textarea.clientHeight;
<a name="line500"></a>  if (!this.scrollHeightIncludesPadding_) {
<a name="line501"></a>    var paddingBox = this.paddingBox_;
<a name="line502"></a>    var paddingBoxHeight = paddingBox.top + paddingBox.bottom;
<a name="line503"></a>    height -= paddingBoxHeight;
<a name="line504"></a>  }
<a name="line505"></a>  if (!this.scrollHeightIncludesBorder_) {
<a name="line506"></a>    var borderBox = goog.style.getBorderBox(textarea);
<a name="line507"></a>    var borderBoxHeight = borderBox.top + borderBox.bottom;
<a name="line508"></a>    height -= borderBoxHeight;
<a name="line509"></a>  }
<a name="line510"></a>  // Prevent negative number results, which sometimes show up.
<a name="line511"></a>  return height &gt; 0 ? height : 0;
<a name="line512"></a>};
<a name="line513"></a>
<a name="line514"></a>
<a name="line515"></a>/**
<a name="line516"></a> * In order to assess the correct height for a textarea, we need to know
<a name="line517"></a> * whether the scrollHeight (the full height of the text) property includes
<a name="line518"></a> * the values for padding and borders. We can also test whether the
<a name="line519"></a> * box-sizing: border-box setting is working and then tweak accordingly.
<a name="line520"></a> * Instead of hardcoding a list of currently known behaviors and testing
<a name="line521"></a> * for quirksmode, we do a runtime check out of the flow. The performance
<a name="line522"></a> * impact should be very small.
<a name="line523"></a> * @private
<a name="line524"></a> */
<a name="line525"></a>goog.ui.Textarea.prototype.discoverTextareaCharacteristics_ = function() {
<a name="line526"></a>  if (!this.hasDiscoveredTextareaCharacteristics_) {
<a name="line527"></a>    var textarea = /** @type {!Element} */ (this.getElement().cloneNode(false));
<a name="line528"></a>    // We need to overwrite/write box model specific styles that might
<a name="line529"></a>    // affect height.
<a name="line530"></a>    goog.style.setStyle(textarea, {
<a name="line531"></a>      &#39;position&#39;: &#39;absolute&#39;,
<a name="line532"></a>      &#39;height&#39;: &#39;auto&#39;,
<a name="line533"></a>      &#39;top&#39;: &#39;-9999px&#39;,
<a name="line534"></a>      &#39;margin&#39;: &#39;0&#39;,
<a name="line535"></a>      &#39;padding&#39;: &#39;1px&#39;,
<a name="line536"></a>      &#39;border&#39;: &#39;1px solid #000&#39;,
<a name="line537"></a>      &#39;overflow&#39;: &#39;hidden&#39;
<a name="line538"></a>    });
<a name="line539"></a>    goog.dom.appendChild(this.getDomHelper().getDocument().body, textarea);
<a name="line540"></a>    var initialScrollHeight = textarea.scrollHeight;
<a name="line541"></a>
<a name="line542"></a>    textarea.style.padding = &#39;10px&#39;;
<a name="line543"></a>    var paddingScrollHeight = textarea.scrollHeight;
<a name="line544"></a>    this.scrollHeightIncludesPadding_ = paddingScrollHeight &gt;
<a name="line545"></a>        initialScrollHeight;
<a name="line546"></a>
<a name="line547"></a>    initialScrollHeight = paddingScrollHeight;
<a name="line548"></a>    textarea.style.borderWidth = &#39;10px&#39;;
<a name="line549"></a>    var borderScrollHeight = textarea.scrollHeight;
<a name="line550"></a>    this.scrollHeightIncludesBorder_ = borderScrollHeight &gt; initialScrollHeight;
<a name="line551"></a>
<a name="line552"></a>    // Tests if border-box sizing is working or not.
<a name="line553"></a>    textarea.style.height = &#39;100px&#39;;
<a name="line554"></a>    var offsetHeightAtHeight100 = textarea.offsetHeight;
<a name="line555"></a>    if (offsetHeightAtHeight100 != 100) {
<a name="line556"></a>      this.needsPaddingBorderFix_ = true;
<a name="line557"></a>    }
<a name="line558"></a>
<a name="line559"></a>    goog.dom.removeNode(textarea);
<a name="line560"></a>    this.hasDiscoveredTextareaCharacteristics_ = true;
<a name="line561"></a>  }
<a name="line562"></a>};
<a name="line563"></a>
<a name="line564"></a>
<a name="line565"></a>/**
<a name="line566"></a> * The CSS class name to add to the input when the user has not entered a
<a name="line567"></a> * value.
<a name="line568"></a> */
<a name="line569"></a>goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS =
<a name="line570"></a>    goog.getCssName(&#39;textarea-placeholder-input&#39;);
<a name="line571"></a>
<a name="line572"></a>
<a name="line573"></a>/**
<a name="line574"></a> * Called when the element goes out of focus.
<a name="line575"></a> * @param {goog.events.Event=} opt_e The browser event.
<a name="line576"></a> * @private
<a name="line577"></a> */
<a name="line578"></a>goog.ui.Textarea.prototype.blur_ = function(opt_e) {
<a name="line579"></a>  if (!this.supportsNativePlaceholder_()) {
<a name="line580"></a>    this.hasFocusForPlaceholder_ = false;
<a name="line581"></a>    if (this.getElement().value == &#39;&#39;) {
<a name="line582"></a>      // Only transition to the default text if we have
<a name="line583"></a>      // no user input.
<a name="line584"></a>      this.hasUserInput_ = false;
<a name="line585"></a>      this.restorePlaceholder_();
<a name="line586"></a>    }
<a name="line587"></a>  }
<a name="line588"></a>};
<a name="line589"></a>
<a name="line590"></a>
<a name="line591"></a>/**
<a name="line592"></a> * Resizes the textarea to grow/shrink to match its contents.
<a name="line593"></a> * @param {goog.events.Event=} opt_e The browser event.
<a name="line594"></a> * @private
<a name="line595"></a> */
<a name="line596"></a>goog.ui.Textarea.prototype.grow_ = function(opt_e) {
<a name="line597"></a>  if (this.isResizing_) {
<a name="line598"></a>    return;
<a name="line599"></a>  }
<a name="line600"></a>  var textarea = this.getElement();
<a name="line601"></a>  // If the element is getting focus and we don&#39;t support placeholders
<a name="line602"></a>  // natively, then remove the placeholder class.
<a name="line603"></a>  if (!this.supportsNativePlaceholder_() &amp;&amp; opt_e &amp;&amp;
<a name="line604"></a>      opt_e.type == goog.events.EventType.FOCUS) {
<a name="line605"></a>    // We must have a textarea element, since we&#39;re growing it.
<a name="line606"></a>    // Remove the placeholder CSS + set the value to empty if we&#39;re currently
<a name="line607"></a>    // showing the placeholderText_ value if this is the first time we&#39;re
<a name="line608"></a>    // getting focus.
<a name="line609"></a>    if (textarea.value == this.placeholderText_ &amp;&amp;
<a name="line610"></a>        this.placeholderText_ &amp;&amp;
<a name="line611"></a>        !this.hasFocusForPlaceholder_) {
<a name="line612"></a>      goog.dom.classlist.remove(
<a name="line613"></a>          textarea, goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS);
<a name="line614"></a>      textarea.value = &#39;&#39;;
<a name="line615"></a>    }
<a name="line616"></a>    this.hasFocusForPlaceholder_ = true;
<a name="line617"></a>    this.hasUserInput_ = (textarea.value != &#39;&#39;);
<a name="line618"></a>  }
<a name="line619"></a>  var shouldCallShrink = false;
<a name="line620"></a>  this.isResizing_ = true;
<a name="line621"></a>  var oldHeight = this.height_;
<a name="line622"></a>  if (textarea.scrollHeight) {
<a name="line623"></a>    var setMinHeight = false;
<a name="line624"></a>    var setMaxHeight = false;
<a name="line625"></a>    var newHeight = this.getHeight_();
<a name="line626"></a>    var currentHeight = textarea.offsetHeight;
<a name="line627"></a>    var minHeight = this.getMinHeight_();
<a name="line628"></a>    var maxHeight = this.getMaxHeight_();
<a name="line629"></a>    if (minHeight &amp;&amp; newHeight &lt; minHeight) {
<a name="line630"></a>      this.setHeight_(minHeight);
<a name="line631"></a>      setMinHeight = true;
<a name="line632"></a>    } else if (maxHeight &amp;&amp; newHeight &gt; maxHeight) {
<a name="line633"></a>      this.setHeight_(maxHeight);
<a name="line634"></a>      // If the content is greater than the height, we&#39;ll want the vertical
<a name="line635"></a>      // scrollbar back.
<a name="line636"></a>      textarea.style.overflowY = &#39;&#39;;
<a name="line637"></a>      setMaxHeight = true;
<a name="line638"></a>    } else if (currentHeight != newHeight) {
<a name="line639"></a>      this.setHeight_(newHeight);
<a name="line640"></a>    // Makes sure that height_ is at least set.
<a name="line641"></a>    } else if (!this.height_) {
<a name="line642"></a>      this.height_ = newHeight;
<a name="line643"></a>    }
<a name="line644"></a>    if (!setMinHeight &amp;&amp; !setMaxHeight &amp;&amp;
<a name="line645"></a>        goog.ui.Textarea.NEEDS_HELP_SHRINKING_) {
<a name="line646"></a>      shouldCallShrink = true;
<a name="line647"></a>    }
<a name="line648"></a>  } else {
<a name="line649"></a>    this.setHeightToEstimate_();
<a name="line650"></a>  }
<a name="line651"></a>  this.isResizing_ = false;
<a name="line652"></a>
<a name="line653"></a>  if (shouldCallShrink) {
<a name="line654"></a>    this.shrink_();
<a name="line655"></a>  }
<a name="line656"></a>  if (oldHeight != this.height_) {
<a name="line657"></a>    this.dispatchEvent(goog.ui.Textarea.EventType.RESIZE);
<a name="line658"></a>  }
<a name="line659"></a>};
<a name="line660"></a>
<a name="line661"></a>
<a name="line662"></a>/**
<a name="line663"></a> * Resizes the textarea to shrink to fit its contents. The way this works is
<a name="line664"></a> * by increasing the padding of the textarea by 1px (it&#39;s important here that
<a name="line665"></a> * we&#39;re in box-sizing: border-box mode). If the size of the textarea grows,
<a name="line666"></a> * then the box is filled up to the padding box with text.
<a name="line667"></a> * If it doesn&#39;t change, then we can shrink.
<a name="line668"></a> * @private
<a name="line669"></a> */
<a name="line670"></a>goog.ui.Textarea.prototype.shrink_ = function() {
<a name="line671"></a>  var textarea = this.getElement();
<a name="line672"></a>  if (!this.isResizing_) {
<a name="line673"></a>    this.isResizing_ = true;
<a name="line674"></a>    var scrollHeight = textarea.scrollHeight;
<a name="line675"></a>    if (!scrollHeight) {
<a name="line676"></a>      this.setHeightToEstimate_();
<a name="line677"></a>    } else {
<a name="line678"></a>      var currentHeight = this.getHeight_();
<a name="line679"></a>      var minHeight = this.getMinHeight_();
<a name="line680"></a>      var maxHeight = this.getMaxHeight_();
<a name="line681"></a>      if (!(minHeight &amp;&amp; currentHeight &lt;= minHeight)) {
<a name="line682"></a>        // Nudge the padding by 1px.
<a name="line683"></a>        var paddingBox = this.paddingBox_;
<a name="line684"></a>        textarea.style.paddingBottom = paddingBox.bottom + 1 + &#39;px&#39;;
<a name="line685"></a>        var heightAfterNudge = this.getHeight_();
<a name="line686"></a>        // If the one px of padding had no effect, then we can shrink.
<a name="line687"></a>        if (heightAfterNudge == currentHeight) {
<a name="line688"></a>          textarea.style.paddingBottom = paddingBox.bottom + scrollHeight +
<a name="line689"></a>              &#39;px&#39;;
<a name="line690"></a>          textarea.scrollTop = 0;
<a name="line691"></a>          var shrinkToHeight = this.getHeight_() - scrollHeight;
<a name="line692"></a>          if (shrinkToHeight &gt;= minHeight) {
<a name="line693"></a>            this.setHeight_(shrinkToHeight);
<a name="line694"></a>          } else {
<a name="line695"></a>            this.setHeight_(minHeight);
<a name="line696"></a>          }
<a name="line697"></a>        }
<a name="line698"></a>        textarea.style.paddingBottom = paddingBox.bottom + &#39;px&#39;;
<a name="line699"></a>      }
<a name="line700"></a>    }
<a name="line701"></a>    this.isResizing_ = false;
<a name="line702"></a>  }
<a name="line703"></a>};
<a name="line704"></a>
<a name="line705"></a>
<a name="line706"></a>/**
<a name="line707"></a> * We use this listener to check if the textarea has been natively resized
<a name="line708"></a> * and if so we reset minHeight so that we don&#39;t ever shrink smaller than
<a name="line709"></a> * the user&#39;s manually set height. Note that we cannot check size on mousedown
<a name="line710"></a> * and then just compare here because we cannot capture mousedown on
<a name="line711"></a> * the textarea resizer, while mouseup fires reliably.
<a name="line712"></a> * @param {goog.events.BrowserEvent} e The mousedown event.
<a name="line713"></a> * @private
<a name="line714"></a> */
<a name="line715"></a>goog.ui.Textarea.prototype.mouseUpListener_ = function(e) {
<a name="line716"></a>  var textarea = this.getElement();
<a name="line717"></a>  var height = textarea.offsetHeight;
<a name="line718"></a>
<a name="line719"></a>  // This solves for when the MSIE DropShadow filter is enabled,
<a name="line720"></a>  // as it affects the offsetHeight value, even with MsBoxSizing:border-box.
<a name="line721"></a>  if (textarea[&#39;filters&#39;] &amp;&amp; textarea[&#39;filters&#39;].length) {
<a name="line722"></a>    var dropShadow =
<a name="line723"></a>        textarea[&#39;filters&#39;][&#39;item&#39;](&#39;DXImageTransform.Microsoft.DropShadow&#39;);
<a name="line724"></a>    if (dropShadow) {
<a name="line725"></a>      height -= dropShadow[&#39;offX&#39;];
<a name="line726"></a>    }
<a name="line727"></a>  }
<a name="line728"></a>
<a name="line729"></a>  if (height != this.height_) {
<a name="line730"></a>    this.minHeight_ = height;
<a name="line731"></a>    this.height_ = height;
<a name="line732"></a>  }
<a name="line733"></a>};
</pre>


</body>
</html>
